<?php
namespace Index\Controller;
use Think\Controller;
header("Content-type: text/html; charset=utf-8");
class FindController extends Controller {
  	//实现分页类
  	public function IndexAction(){  
  		$cid = I('get.cid');
		 $model = M();
  		 $sql = "select count(*) cn from question";
  		 if(!empty($cid)){
  		 	$sql = "select count(*) cn from question where cid ='{$cid}'";
  		 }

  		 $count = current($model->query($sql))['cn'];//取得问题中的总条数
 		
 		 $Page  = new \Think\Page($count,4);//实例化Page对象
 		 $show  = $Page->show();// 分页显示输出
 		 if(empty($cid)){
 		 	$sql = "select t1.*,t2.username,t2.head_img,t3.cname from question t1 left join user t2 on t1.uid=t2.id left join category t3 on t1.cid=t3.id  order by t1.time desc limit {$Page->firstRow},{$Page->listRows}";
 		 }else{
 		 	$sql = "select t1.*,t2.username,t2.head_img from question t1 left join user t2 on t1.uid=t2.id left join category t3 on t1.cid=t3.id where t1.cid='{$cid}' order by t1.time desc limit {$Page->firstRow},{$Page->listRows}";
 		 }
 		 $newlist = $model->query($sql);//连表查询
 		 //类别查询
 		 $category_sql = "select * from category"; 
 		 $category_list = $model->query($category_sql);
 		 //文章评论人头像
 		 $data_m=M();
 		 foreach ($newlist as $key => $value) { 
 		  	$questionId=$value['id'];
 		  	// $uid = $value['uid'];
 		  	$sql="select distinct t2.head_img from answer t1 left join user t2 on t1.uid=t2.id where qid='$questionId'";
 		  	$user_img=$data_m->query($sql);
 		  	$newlist[$key]['replay'] = $user_img;
 		  } 

 		//热门用户
 		  $userid_sql="select id from user";
 		  $userid_res=$data_m->query($userid_sql);
 		  $arr = array();
 		  $i=0;
		  foreach ($userid_res as $key => $value) {		  	 
		  	 $uid=$value['id'];
		  	 $qid_sql="select count(*) as num,t2.username,t2.head_img from question t1 left join user t2 on t1.uid=t2.id where uid='$uid'";
		  	 $qid_res=$data_m->query($qid_sql);
		  	 
		  	 // exit();
		  	 $qid_res=current($qid_res);
		  	 $arr[$i]=$qid_res;
		  	 $i++;		  	 
		  }
		  // var_dump($newlist);
 		 // 	exit();		 
 		 // 输出至模板
 		 $this->assign('arr',$arr);// 热门用户输出到模板
 		 $this->assign('page',$show);// 赋值分页输出到模板
		 $this->assign('newlist',$newlist);// 连表查询结果输出到模板
		 $this->assign("category_list",$category_list);	// 类别查询结果输出到模板
		 
		 $this->display('index');
  		 
	}

	//获取文章的详细信息和回复信息
	public function moreDetailAction(){		
		if(IS_POST){	//查看文章中是否被传值，接收表单中的数据对文章评论
			$qid = I('POST.question_id');
			// var_dump($qid);
			// exit();
			$centent = I('POST.answer_content');
			$uid = $_SESSION['id'];
			$time = date("Y-m-d H:i:s");
			$data_m=M();
			$replaynum_sql="update question set replay_num=replay_num+1 where id='$qid'";
			$replaynum_res=$data_m->execute($replaynum_sql);
			if($replaynum_res){
				// var_dump($replaynum_res);
				// exit();
				// $sql = "insert into answer(centent,time,qid,uid) values('$centent','$time','$qid','$uid')";
				// $replay_res = $data_m->execute($sql);
				$data['centent']=$centent;
				$data['time']=$time;
				$data['qid']=$qid;
				$data['uid']=$uid;
				$replay_m=M("answer");
				$replay_res=$replay_m->add($data);
				// var_dump($replay_res);
				// exit();
				if($replay_res){	


					//发表文章成功后需要回复行为
					$uid=$uid;
                    $qid=$qid;
                    $send_replay_question="回答了问题";
                    $replay_id=$replay_res;
                    $time=date('Y-m-d H:i:s');
                    $data_m=M();
                    $sql="insert into log(uid,qid,send_replay_question,replay_id,time) value('$uid','$qid','$send_replay_question','$replay_id','$time')";
                    $insert_res=$data_m->execute($sql);
                    //记录行为操作结束

                    if($insert_res){
                    	$this->success('回复成功',U('index/Find/moreDetail',array('id'=>$qid)),2);
                    }
					
				}else{
				    $this->error("回复失败",U('index/Find/moreDetail'),2);
				    }
			}		

		}elseif(I('get.id')){
				// var_dump(I('get.id'));
				// exit();
				$titleid = I('get.id');//获取传过来的文章ID
				$data_m = M();
				$viewNum_sql="update question set view=view+1 where id='$titleid'";
				$viewNum_res=$data_m->execute($viewNum_sql);
				// var_dump($viewNum_res);
				// exit();
			if($viewNum_res){
				$sql = "select t1.title,t1.centent,t1.id as qid,t1.replay_num,t1.time,t2.username,t2.head_img  from question t1 left join user t2 on t1.uid=t2.id  where t1.id='$titleid'";
		  	
			  	$Res_info= $data_m->query($sql);
			  	$sql = "select * from answer t1 left join user t2 on t1.uid=t2.id where t1.qid='{$titleid}'";
			  	$data= $data_m->query($sql);
			  	$Res_info_time=$Res_info[0]['time'];
			  	$between_time_stamp=time()-$Res_info_time;
			  	if($between_time_stamp>(60*60*24)){
			  		$Res_info[0]['time']=floor($between_time_stamp/(60*60*24)).'天前';
			  	}elseif($between_time_stamp>(60*60)){
			  		$Res_info[0]['time']=floor($between_time_stamp/(60*60)).'小时前';
			  	}elseif($between_time_stamp>60){
			  		$Res_info[0]['time']=floor($between_time_stamp/60).'分钟前';
			  	}
			  	// var_dump($Res_info);
			  	// exit();
			  	$this->assign("data",$data);
			  	$this->assign("Res_info",$Res_info);
				$this->display();
			}	  	
		}
		
	}


	//评论赞同
	public function agreeAction(){	
		$username=I('post.user_name');
		$answer_id=I('post.answer_id');
		//查出用户ID
		$sql="select id from user where username='$username'";
		$data_m=M();
		$res=$data_m->query($sql);
		$uid=$res[0]['id'];

		//插入用户点赞表
		$agree_sql="insert into answer_agree(aid,uid) values('$answer_id','$uid')";
		$res = $data_m->execute($agree_sql);
		if($res){
			$answer_agree_sql="update answer set agree='1'";
			$res = $data_m->execute($answer_agree_sql);
			$arr = array('success' =>$res);
			echo json_encode($arr);
		}

	}

	//文章关注
	public function questionFocusAction(){
			var_dump(I('get.'));
			exit();

		}



}
?>